package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.dao.mapper.ShopProductSkuMappingEntityMapper;
import com.linkhub.oms.dao.mpservice.IShopProductSkuMappingService;
import com.linkhub.oms.entity.ShopProductSkuMappingEntity;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 商店商品sku表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-20
 */
@Service
public class ShopProductSkuMappingServiceImpl extends ServiceImpl<ShopProductSkuMappingEntityMapper, ShopProductSkuMappingEntity> implements IShopProductSkuMappingService {

    @Override
    public void deleteMapping( String sysShopId, Long shopSkuId ) {
        super.lambdaUpdate()
                .eq(ShopProductSkuMappingEntity::getSysShopId, sysShopId)
                .eq(ShopProductSkuMappingEntity::getShopSkuId, shopSkuId)
                .remove();
    }

    @Override
    public List<ShopProductSkuMappingEntity> listSkuMappings( String sysShopId, Collection<Long> shopSkuIds ) {
        if (CollUtil.isEmpty(shopSkuIds)) {
            return Lists.newArrayListWithCapacity(0);
        }

        return super.lambdaQuery()
                .select(ShopProductSkuMappingEntity::getSysShopId,
                        ShopProductSkuMappingEntity::getShopSkuId,
                        ShopProductSkuMappingEntity::getSkuId)

                .eq(ShopProductSkuMappingEntity::getSysShopId, sysShopId)
                .in(ShopProductSkuMappingEntity::getShopSkuId, shopSkuIds)
                .list();
    }

    @Override
    public ShopProductSkuMappingEntity getByShopSkuId( Long shopSkuId ) {
        if (shopSkuId == null) {
            return new ShopProductSkuMappingEntity();
        }
        return super.lambdaQuery()
                .in(ShopProductSkuMappingEntity::getShopSkuId, shopSkuId)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public ShopProductSkuMappingEntity getBySkuId( String skuId ) {
        ShopProductSkuMappingEntity shopProductSkuMappingEntity = super.lambdaQuery()
                .in(ShopProductSkuMappingEntity::getSkuId, skuId)
                .last("LIMIT 1")
                .one();
        if (shopProductSkuMappingEntity == null) {
            return new ShopProductSkuMappingEntity();
        }
        return shopProductSkuMappingEntity;
    }
}
